package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.domain.SysDictData;
import com.huike.clues.domain.SysDictType;
import com.huike.clues.domain.dto.SysDictTypeQueryDTO;
import com.huike.clues.mapper.SysDictDataMapper;
import com.huike.clues.mapper.SysDictTypeMapper;
import com.huike.clues.service.ISysDictTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements ISysDictTypeService {
    private static final String DICTTYPE_ALL = "DictType:all";
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询字典类型列表
     *
     * @param sysDictTypeQueryDTO
     * @return
     */
    @Override
    public List<SysDictType> selectDictTypeList(SysDictTypeQueryDTO sysDictTypeQueryDTO) {
        //先去redis查询
        List<SysDictType> list = (List<SysDictType>) redisTemplate.opsForValue().get(DICTTYPE_ALL);
        if (list != null && list.size() > 0) {
            return list;
        }
        // 缓存中没有数据，从数据库中查询
        list = sysDictTypeMapper.selectDictTypeList(sysDictTypeQueryDTO);
        //存到redis
       /* redisTemplate.opsForValue().set(DICTTYPE_ALL, list);*/

        return list;
    }

    /**
     * 新增字典类型
     *
     * @param sysDictType
     */
    @Override
    public void add(SysDictType sysDictType) {
        // 应该设置创建时间等字段
        sysDictType.setCreateTime(new Date());
        this.save(sysDictType);
        deleteCache();
    }

    /**
     * 删除字典类型和 数据
     *
     * @param dictIds
     */
    @Override
    public void removetypeAndDate(String dictIds) {
        //判断是否有数据绑定，有则不能删除，否则可以删除
        SysDictType byId = this.getById(dictIds);

        List<SysDictData> list = sysDictDataMapper.selectList(Wrappers.<SysDictData>lambdaQuery().eq(SysDictData::getDictType, byId.getDictType()));
        if (CollectionUtils.isNotEmpty(list)) {
            throw new RuntimeException("该字典下有数据，不可以删除");
        }
        this.removeById(dictIds);
        deleteCache();
    }

    /**
     * 修改数据类型
     *
     * @param sysDictType
     */
    @Override
    public void updatedicttype(SysDictType sysDictType) {
        this.updateById(sysDictType);
    }

    /**
     * 删除缓存
     */
    @Override
    public void clearCache() {
        deleteCache();
    }

    private void deleteCache() {
        // 删除缓存
        redisTemplate.delete(DICTTYPE_ALL);
        redisTemplate.delete(DICTTYPE_ALL);
    }

    /**
     * 获取字典选择框
     *
     * @return
     */
    /*@Override
    public List<SysDictType> listdict() {
        return sysDictTypeMapper.listdict();
}*/
}